from django.db import models

from apps.accounts.models import User
from bases.model import APIModel


class SerialNumberModel(APIModel):
    serial_number = models.CharField(verbose_name='制番号', max_length=12, unique=True)
    serial_name = models.CharField(verbose_name='制番名称', max_length=50, blank=True, null=True)
    serial_department = models.ManyToManyField('master.DepartmentsModel', verbose_name='拥有的所有部门',
                                         related_name='serial_department', blank=True)  # 定义反向查询名departments

    class Meta:
        db_table = 'SERIALNUMBER'
        verbose_name = '制番主表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.serial_number


class DepartmentsModel(APIModel):
    code  = models.CharField(verbose_name='部门代码', max_length=3, unique=True)
    name = models.CharField(verbose_name='部门名称', max_length=10)

    class Meta:
        db_table = 'DEPARTMENTS'
        verbose_name = '部门主表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 这个model暂时没有使用到，目前是在user的model中使用了多对多字段来实现了，部门与用户之间的关联
class UserDepartmentsModel(APIModel):
    departmentFK = models.ForeignKey(DepartmentsModel, on_delete=models.DO_NOTHING)
    userFK = models.ForeignKey(User, on_delete=models.DO_NOTHING)

    class Meta:
        db_table = 'USERDEPARTMENT'
        verbose_name = '用户部门分组表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.id

class CurrencyModel(APIModel):
    currency = models.CharField(verbose_name='币种', max_length=3, unique=True)
    currency_name = models.CharField(verbose_name='币种名称', max_length=20, blank=True, null=True)
    currency_country = models.CharField(verbose_name='币种国家', max_length=30, blank=True, null=True)

    class Meta:
        db_table = 'CURRENCY'
        verbose_name = '币种主表'
        verbose_name_plural = verbose_name

class CurrencyRateModel(APIModel):
    standard_curr = models.CharField(verbose_name='标准币种', max_length=3)
    quote_curr = models.CharField(verbose_name='报价币种', max_length=3)
    rate_date = models.DateField(verbose_name='汇率日期',null=False)
    exchange_rate = models.DecimalField(verbose_name='汇率', max_digits=12, decimal_places=6,default=1)

    class Meta:
        db_table = 'CURRENCYRATEM'
        verbose_name = '币种换算主表'
        verbose_name_plural = verbose_name
        # unique_together = ['standard_curr', 'quote_curr', 'rate_date']

    def __str__(self):
        return self.standard_curr


class SystemParametersModel(APIModel):
    param_code = models.CharField(verbose_name='参数Code', max_length=20, unique=True)
    param_name = models.CharField(verbose_name='参数名称', max_length=30, blank=True, null=True)
    param_value = models.CharField(verbose_name='参数值', max_length=100, blank=True, null=True)

    class Meta:
        db_table = 'SYSTEMPARAMETER'
        verbose_name = '系统参数表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.param_code